package NW_MC;

import java.util.ArrayList;
import java.util.List;

import NWPackage.ProbabilityCell;
import Writers.MsgLog;

public class RandomMatrix {
	
	String[] candidates;
	int[] sumOfVotes;
	
	public RandomMatrix(List<String> optionalWinners) {
		candidates = (String[]) optionalWinners.toArray(new String[0]);
		sumOfVotes = new int[optionalWinners.size()];
		
	}

	public void setCell(ProbabilityCell probability) throws Exception {
		//String agentName = probability.getAgentName();
		for (int i = 0; i<candidates.length; i++) {
			try{
				String index = candidates[i];
				int x = probability.getIndexOf(index);
				sumOfVotes[i] += x + 1;	
			}
			catch(Exception ex){
				System.out.println(ex.getMessage());
				MsgLog.write_to_init(ex.getMessage());
			}
		}
	}

	
	// returns a list of winner cands, that is - cands with the highest sumOfVotes
	public List<String> getWinnerCandidate() {
		int max = 0;
		List<String> cands = new ArrayList<String>();	
		for (int i = 0; i<sumOfVotes.length; i++) {
			if (sumOfVotes[i] > max)
			{
				max = sumOfVotes[i];
				cands.clear();
				cands.add(candidates[i]);				
			}
			else if (sumOfVotes[i] == max){
				cands.add(candidates[i]);
			}
		}
		return cands;
	}

}